package com.smartplatform.controller;

import com.github.pagehelper.PageInfo;
import com.smartplatform.dto.DeviceDto;
import com.smartplatform.dto.R;
import com.smartplatform.operlog.annotations.LogAnnotation;
import com.smartplatform.pojo.SysDevice;
import com.smartplatform.service.SysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 咕唧
 * @Date 2022/5/30 21:38
 * @Version 1.0
 */
@RequestMapping("/devices")
@RestController
public class SysDeviceController {
    private SysDeviceService sysDeviceService;

    @Autowired
    public void setSysDeviceService(SysDeviceService sysDeviceService) {
        this.sysDeviceService = sysDeviceService;
    }

    @PostMapping("/device")
    public R addDevice(@RequestBody DeviceDto DeviceDto) {
        sysDeviceService.addDevice(DeviceDto);
        return R.setOK();
    }

    @PostMapping("/device/sell/{deviceId}")
    public R updateSell(@PathVariable String deviceId) {
        sysDeviceService.updateSell(deviceId);
        return R.setOK();
    }

    @PostMapping("/device/bind/{deviceId}/{sceneId}")
    public R bindDevice(@PathVariable String deviceId, @PathVariable Long sceneId) {
        sysDeviceService.bindDevice(deviceId, sceneId);
        return R.setOK();
    }

    @LogAnnotation(title = "查询所有设备",businessType = 4)
    @GetMapping("/devices")
    public R getDevices(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        PageInfo<SysDevice> pageInfo = sysDeviceService.findAllDevices(page, limit);
        return R.setOK(pageInfo);
    }

    @LogAnnotation(title = "发送指令",businessType = 0)
    @PostMapping("/command/{deviceId}/{command}")
    public R sendControl(@PathVariable String deviceId,@PathVariable  String command) {
        sysDeviceService.sendControl(deviceId, command);
        return R.setOK();
    }

}
